<?php
		//#TODO this should all be handled higher up
		$syncItem = new Rd_Synchronizer_ItemPhysicalCopies($this->item);
		$ilsCopies = $syncItem->getIlsCopies();
		$internalPhysicalCopies = $syncItem->getInternalCopies();
?>
	<div class="displayArea">
<?php if ($this->ils->supportsItemLookup()) { 
		$inSync = $syncItem->isSynced();
		$outOfSyncCopies = $syncItem->getOutOfSyncCopies();
		$missingCopies = $syncItem->getMissingIlsCopies();
		$unsyncableCopies = $syncItem->getUnsyncableCopies();
		$purgableCopies = $syncItem->getPurgableCopies();
	?>
		<div class="headingCell1">ILS SYNC</div>
		<div id="ils_sync" style="padding:8px 8px 12px 8px;">

<?php 		if ('' != $this->item->getControlNumber()) { ?>
	    	<p><?php print(count($ilsCopies) 
	    		. ' cop' . (count($ilsCopies) == 1 ? 'y' : 'ies') 
	    		. ' of this title in the ILS.'); ?></p>
	    	<p><?php print(
	    		count($missingCopies) == 0 
	    		? 'No missing copies from the ILS.' 
	    		: (count($missingCopies) . ' ILS  cop' 
	    		. (count($missingCopies) == 1 ? 'y' : 'ies') 
	    		. ' missing from the internal database.') 
	    	); ?></p>
	    	<?php 
	    		if(count($outOfSyncCopies) > 0 || count($missingCopies) > 0) { 
	    			print(
		    		count($outOfSyncCopies) . ' internal  cop' 
		    		. (count($outOfSyncCopies) == 1 ? 'y' : 'ies') 
		    		. ' out of sync.' 
		    	); ?></p>
		    	
		    	<form action="./?cmd=syncPhysicalCopy" method="post">
		    		<input type="submit" name="syncAllCopies" value="Copy/Sync All"/>
		    		<input type="hidden" name="itemId" value="<?php print($this->item->getId());?>"/>
		    	</form> 
	    	<?php } 
			} else {
				$outOfSyncCopies = array();
				$itemCopies = $this->item->getPhysicalCopies();
				$unsyncableCopies = array();
				foreach($itemCopies as $itemCopy) {
					$unsyncableCopies[$itemCopy->getId()] = $itemCopy;
				}
	?>
	<p>This item has no Control Number. To sync physical copies to the ILS, edit this item, providing a Control Number.</p>
	<?php 
			}
	    	?>
		</div>
<?php 
		} else {
			$outOfSyncCopies = array();
			$itemCopies = $this->item->getPhysicalCopies();
			$unsyncableCopies = array();
			foreach($itemCopies as $itemCopy) {
				$unsyncableCopies[$itemCopy->getId()] = $itemCopy;
			}
			$purgableCopies = array();
			
		}
?>
		<div class="headingCell1">INTERNAL INVENTORY</div>
		<div id="internal_copies" style="padding:8px 8px 12px 8px;">
<?php if (count($internalPhysicalCopies) == 0) {?>
			<p>This item has no physical copies currently</p>
<?php } ?>
			<form action="./?cmd=createPhysicalCopy" method="post">
				<input type="submit" name="createPhysicalCopy" value="Add a Physical Copy"/>
				<input type="hidden" name="itemId" value="<?php print($this->item->getId());?>"/>
			</form>
			<br/>
<?php  foreach($internalPhysicalCopies as $physicalCopy) { 
			$currentId = $physicalCopy->getId();
			$outOfSync = array_key_exists($currentId, $outOfSyncCopies);
			$unsyncable = array_key_exists($currentId, $unsyncableCopies);
			$purgable = array_key_exists($currentId, $purgableCopies);
			$syncClass = (
				($outOfSync && $this->ils->supportsItemLookup())
				|| ($purgable && $this->ils->supportsItemLookup())
				? ' outOfSync'
				: (
					$unsyncable && $this->ils->supportsItemLookup()
					? ' cannotSync'
					: ''
				)
			);
?>			<div class="bordered appOptions bottomMargin <?php print($syncClass)?>">
				<h3><?php print(
					$outOfSync || $purgable
					? 'Out of Sync Copy' . (
						$purgable
						? " - Mismatched Barcode"
						: ''
					)
					: (
						$unsyncable
						? ($this->ils->supportsItemLookup() ? 'Copy Not In ILS' : 'Internal Copy')
						: 'In Sync Copy'
					)
				); ?></h3>
			<form action="./?cmd=editPhysicalCopy" method="post" class="grid_10">
				<input type="hidden" name="physicalCopyId" value="<?php print($currentId);?>"/>
				<input type="hidden" name="itemId" value="<?php print($this->item->getId());?>"/>
				<div class="grid_6">
				<label class="blockLabel">Barcode: <input name="barcode" type="text" id="barcode" size="30" value="<?php print($physicalCopy->getBarcode()); ?>" /></label>
				<label class="blockLabel">Call Number:<input name="call_num" type="text" id="call_num" size="30" value="<?php print($physicalCopy->getCallNumber()); ?>" /></label>
				<label class="blockLabel disabled">Shelving Key:<input name="shelving_key" disabled="disabled" type="text" size="30" value="<?php print($physicalCopy->getShelvingKey()); ?>" /></label>
				<br/>
				<input type="submit" name="updatePhysicalCopy" value= "Update This Copy"/>
				</div>
				<div class="grid_4">
				<label class="blockLabel">Library:<input name="library" type="text" size="30" value="<?php print($physicalCopy->getOwningLibrary()); ?>" /></label>
				<label class="blockLabel">Status:<input name="status" type="text" size="30" value="<?php print($physicalCopy->getStatus()); ?>" /></label>
				<label class="blockLabel">Type:<input name="type" type="text" size="30" value="<?php print($physicalCopy->getItemType()); ?>" /></label>
				</div>
			</form>
			<form action="./?cmd=removePhysicalCopy" method="post" class="grid_3">
				<?php 
					$owner = $physicalCopy->getOwnerUserID();
					if (!is_null($owner) && 0 != $owner) {
						$ownerObject= new user($owner);
						$ownerName = $ownerObject->getFirstName() . ' ' . $ownerObject->getLastName();
						$ownerEmail = $ownerObject->getEmail();
					?>
						<span><img src="<?php Rd_Layout::printIconUrl('book_user'); ?>" alt='' />Personal Copy of <a href="mailto:<?php print($ownerEmail);?>"><?php print($ownerName);?></a></span>
					<?php } ?>
				<input type="submit" name="removePhysicalCopy" value="Remove This Copy"/>
				<input type="hidden" name="itemId" value="<?php print($this->item->getId());?>"/>
				<input type="hidden" name="physicalCopyId" value="<?php print($currentId);?>"/>
			</form>
			<div class="clear"></div>
			</div>
<?php } ?>
		</div>
	</div>
<?php 